package dao;

import model.User;
import util.DBConn;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.*;


public class Db {
    public List<User> getAllData ()  {
        ArrayList<User> users = new ArrayList<>();
        Connection conn = DBConn.getConn();
        String sql="select * from user";
//        预编译
        try {
            PreparedStatement statement = conn.prepareStatement(sql);
            //执行
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()){
                User user = new User();
                int id=resultSet.getInt(1);
                String username=resultSet.getString(2);
                String password=resultSet.getString(3);
                String birthday=resultSet.getString(4);

                user.setId(id);
                user.setUsername(username);
                user.setPassword(password);
                user.setBirthday(birthday);
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }
//    删除
    public int del(String id){
        Connection conn = DBConn.getConn();
        String sql="delete from user where id=?";
        int i=0;
        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1,id);
            i=preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return i;
    }

//    添加
    public int add(User user){
        Connection conn = DBConn.getConn();
        String sql="insert into user(username,password,birthday)values(?,?,?)";
        int i=0;
        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, user.getUsername());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.setString(3, user.getBirthday());
            i=preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
    //更新首先得到数据
    public User getOneData(String id){
        Connection conn = DBConn.getConn();
        String sql="select * from user where id=?";
        User user=null;
        try {
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1,id);
            ResultSet resultSet=statement.executeQuery();
            resultSet.next();
            user = new User();
            user.setId(resultSet.getInt(1));
            user.setUsername(resultSet.getString(2));
            user.setPassword(resultSet.getString(3));
            user.setBirthday(resultSet.getString(4));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }
    //更新一条数据
    public int updateOneData(User user){
        Connection conn = DBConn.getConn();
        String sql="update user set username=?,password=?,birthday=? where id=?";
        int i=0;
        try {
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1,user.getUsername());
            statement.setString(2, user.getPassword());
            statement.setString(3,user.getBirthday());
            statement.setInt(4,user.getId());
            i=statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
    //判断
    public Boolean islogin(String admin, String password) {
        try {
            final Connection conn = DBConn.getConn();
            String sql = "select* from manager where admin=? and password=?";

            final PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1,admin);

            preparedStatement.setString(2,password);
            final ResultSet resultSet = preparedStatement.executeQuery();

            return resultSet.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}
